package com.foton.lotteryinterfaces.interfaces.faced.impl;


import com.foton.lotteryapplication.process.req.DrawProcessReq;
import com.foton.lotteryapplication.process.res.DrawProcessResult;
import com.foton.lotteryapplication.process.res.RuleQuantificationCrowdResult;
import com.foton.lotteryapplication.process.service.IActivityProcess;
import com.foton.lotterycommon.common.Constants;
import com.foton.lotterycommon.common.Result;
import com.foton.lotterydomain.rule.model.req.DecisionMatterReq;
import com.foton.lotterydomain.strategy.model.vo.DrawAwardInfo;
import com.foton.lotteryinfrastructure.infrastru.dao.IActivityDao;
import com.foton.lotteryinfrastructure.infrastru.po.Activity;
import com.foton.lotteryinterfaces.interfaces.assembler.AwardMapping;
import com.foton.lotteryinterfaces.interfaces.faced.IAcitiveBoothInterfaces;
import com.foton.lotteryrpc.rpc.IAcitiveBooth;
import com.foton.lotteryrpc.rpc.dto.ActivityDto;
import com.foton.lotteryrpc.rpc.dto.AwardDTO;
import com.foton.lotteryrpc.rpc.req.ActivityReq;
import com.foton.lotteryrpc.rpc.req.DrawReq;
import com.foton.lotteryrpc.rpc.req.QuantificationDrawReq;
import com.foton.lotteryrpc.rpc.res.ActivityRes;
import com.foton.lotteryrpc.rpc.res.DrawRes;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

//import com.foton.lotterycommon.common.Result;

/**
 * @Author RenPu
 * @Date 2023/11/22 15:14
 * @Version 1.0
 * @Description: 营销活动业务逻辑实现层
 **/

@Service
public class ActivityBoothImpl implements IAcitiveBooth {

    @Resource
    public IActivityDao acitiveBoothDao;

    @Resource
    private IActivityProcess iActivityProcess;

    @Resource
    private AwardMapping awardMapping;


    @Override
    public ActivityRes queryIAcititve(ActivityReq activityReq) {

        Activity activity = acitiveBoothDao.queryActivityById(activityReq.getActivityId());
        ActivityDto activityDto = new ActivityDto();
        activityDto.setActivityId(activity.getActivityId());
        activityDto.setActivityName(activity.getActivityName());
        activityDto.setActivityDesc(activity.getActivityDesc());
        activityDto.setBeginDateTime(activity.getBeginDateTime());
        activityDto.setEndDateTime(activity.getEndDateTime());
        activityDto.setStockCount(activity.getStockCount());
        activityDto.setTakeCount(activity.getTakeCount());
        return new ActivityRes(new Result(Constants.ResponseCode.SUCCESS.getCode(), Constants.ResponseCode.SUCCESS.getInfo()), activityDto);
    }


    /**
     * 直接抽奖
     * @param drawReq 请求参数
     * @return
     */
    @Override
    public DrawRes doDraw(DrawReq drawReq) {

    //1:执行抽奖
        DrawProcessResult processResult = iActivityProcess.doDrawProcess(new DrawProcessReq(drawReq.getuId(), drawReq.getActivityId()));

    //2:判断抽奖结果是否成功
       if(!Constants.ResponseCode.SUCCESS.getCode().equals(processResult.getCode())){
           return new DrawRes(processResult.getCode(),processResult.getInfo());
       }

    //3:数据封装，响应出去
    DrawRes drawRes = new DrawRes(Constants.ResponseCode.SUCCESS.getCode(), Constants.ResponseCode.SUCCESS.getInfo());
    DrawAwardInfo drawAwardInfo = processResult.getDrawAwardInfo();
    AwardDTO awardDTO = awardMapping.sourceToTarget(drawAwardInfo); //对象转换
    drawRes.setAwardDTO(awardDTO);
    return drawRes;
    }


    /**
     * 规则量化后，进行抽奖
     * @param quantificationDrawReq 请求参数
     * @return
     */
    @Override
    public DrawRes doQuantificationDraw(QuantificationDrawReq quantificationDrawReq) {

        //1:获取规则量化后得活动ID
        RuleQuantificationCrowdResult ruleQuantificationCrowdResult = iActivityProcess.doRuleQuantificationCrowd(new DecisionMatterReq(quantificationDrawReq.getTreeId(), quantificationDrawReq.getuId()));

        //2:校验规则量化结果是否成功
        if(!Constants.ResponseCode.SUCCESS.getCode().equals(ruleQuantificationCrowdResult.getCode())){
           return new DrawRes(ruleQuantificationCrowdResult.getCode(),ruleQuantificationCrowdResult.getInfo());
        }

        //3:获取规则量化后得活动ID，交由执行抽奖流程执行
        DrawProcessResult drawProcessResult = iActivityProcess.doDrawProcess(new DrawProcessReq(quantificationDrawReq.getuId(),ruleQuantificationCrowdResult.getActivityId()));

        //4:判断抽奖结果是否成功
        if(!Constants.ResponseCode.SUCCESS.getCode().equals(drawProcessResult.getCode())){
            return new DrawRes(drawProcessResult.getCode(),drawProcessResult.getInfo());
        }

        //5:数据封装，响应出去
        DrawRes drawRes = new DrawRes(Constants.ResponseCode.SUCCESS.getCode(), Constants.ResponseCode.SUCCESS.getInfo());
        DrawAwardInfo drawAwardInfo = drawProcessResult.getDrawAwardInfo();
        AwardDTO awardDTO = awardMapping.sourceToTarget(drawAwardInfo); //对象转换
        drawRes.setAwardDTO(awardDTO);
        return drawRes;
    }
}
